﻿using System;
using System.Collections.Generic;
using System.Linq;

#if WINDOWS_PHONE71
namespace WindowsPhoneMVC.Navigation
{
    public static class DeepLink
    {
        public static string UriFor(string controller, string action, IDictionary<string, string> parameters)
        {
            var parametersString = string.Join("&", parameters.Select(p => p.Key + "=" + p.Value));
            return string.Format("/Shell.xaml?controller={0}&action={1}{2}{3}", controller, action, 
                string.IsNullOrEmpty(parametersString) ?  string.Empty : "&", parametersString);
        }

        public static NavigationRequest DecodeUri(string navigationFrame, Dictionary<string, string> deepLinkQueryString)
        {
            string controller = null;
            if (deepLinkQueryString.ContainsKey("controller"))
            {
                controller = deepLinkQueryString["controller"];
                deepLinkQueryString.Remove("controller");
            }

            string action = null;
            if (deepLinkQueryString.ContainsKey("action"))
            {
                action = deepLinkQueryString["action"];
                deepLinkQueryString.Remove("action");
            }

            return new NavigationRequest(navigationFrame, controller, action, new NavigationParameter(deepLinkQueryString));
        }
    }
}
#endif
